<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">
      <el-form-item label="服务商品名称:" prop="name">
        <el-input v-model="queryData.name" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('name')"/>
      </el-form-item>

      <el-form-item label="服务提供方名称:" prop="providerName">
        <el-input v-model="queryData.providerName" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('providerName')"/>
      </el-form-item>


      <el-form-item label="联系方式:" prop="providerPhone">
        <el-input v-model="queryData.providerPhone" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('providerPhone')"/>
      </el-form-item>

      <el-form-item label="类型:" prop="type">
        <el-select v-model="queryData.type" placeholder="请选择" clearable @clear="formClear('type')"
                   style="width: 200px">
          <el-option :label="item.name" :value="item.code" v-for="(item, index) in typeList" :key="index"/>
        </el-select>
      </el-form-item>

      <el-form-item label="添加时间:" prop="createTime">
        <el-date-picker v-model="queryData.createTime" value-format="yyyy-MM-dd" type="date" style="width: 200px"
                        @clear="formClear('createTime')" placeholder="选择日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="$router.push('/serviceCommodityManage/serviceCommodityAdd')">新 增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column prop="id" label="服务商品ID" align="center" width="90"/>
      <el-table-column prop="name" label="服务商品名称" align="center" width="200" show-overflow-tooltip/>
      <el-table-column prop="providerName" label="服务提供方名称" align="center" width="150"/>
      <el-table-column prop="providerPhone" label="服务方联系方式" align="center" width="150"/>
      <el-table-column prop="norms" label="规格" align="center" width="150"/>
      <el-table-column prop="price" label="价格" align="center" width="150"/>
      <el-table-column prop="typeName" label="类型" align="center" width="150"/>
      <el-table-column prop="visitPath" label="页面路径" align="center" width="200" show-overflow-tooltip :formatter="(row) => `${row.visitPath}?detailId=${row.id}`"/>
      <el-table-column prop="visitsAmount" label="访问次数" align="center"/>
      <el-table-column prop="purchaseAmount" label="购买数量" align="center"/>
      <el-table-column prop="shelfStatus" label="上架状态" align="center">
        <template slot-scope="{row}">
          <el-switch :value="row.shelfStatus"
                     :active-value="1" :inactive-value="0"
                     @change="switchChange($event, row, 'shelfStatus')"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text" @click="entityInfoEvent(row)">查 看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    computed: {
      ...mapGetters(['confirm'])
    },
    name: 'serviceCommodityList',
    data() {
      return {
        typeList: [],
        tableList: [],
        queryData: {
          createTime: null, // 创建时间
          name: null, // 商品名字
          providerName: null, // 服务提供商名称
          providerPhone: null, // 提供商联系方式
          type: null, // 类型 (资源表code)
          pageNum: 1,
          pageSize: 10,
          total: 0
        }
      }
    },
    beforeMount() {
      this.getRequest()
      this.getTypeList()
    },
    methods: {
      /**
       *  开关事件
       */
      async switchChange(event, row, key) {
        this.$confirm('确认修改该条数据吗?', '提示', this.confirm.options).then(async() => {
          await this.$ajax.post('/wx/serviceGoods/update', {
            id: row.id,
            [key]: event
          }).finally(() => this.getRequest())
          this.$message.success('操作成功')
        })
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },
      /**
       * 重置事件
       */
      onReset() {
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 列表请求事件
       */
      async getRequest() {
        const queryData = JSON.parse(JSON.stringify(this.queryData))
        const res = await this.$ajax.get('/wx/serviceGoods/backstage/list', { params: queryData })
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      /**
       * 获取类型
       */
      async getTypeList() {
        const res = await this.$ajax.get('/resource/table/list?type=fuwushangpintype')
        this.typeList = res.rows
      },
      /**
       * 删除事件
       */
      deleteEvent(row) {
        this.$confirm('确认要删除该数据吗?', '提示', this.confirm.options).then(async() => {
          await this.$ajax.get(`/wx/poster/remove?id=${row.id}`)
          this.$message.success('操作成功!')
          this.getRequest()
        })
      },
      /**
       * 跳转详情
       */
      entityInfoEvent(row) {
        this.$router.push(`/serviceCommodityManage/serviceCommodityAdd?id=${row?.id || ''}`)
      },
      /**
       * form置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      }
    }
  }
</script>

<style scoped>

</style>
